package exporter;

import org.apache.commons.io.IOUtils;
import org.apache.hadoop.hbase.client.HTableInterface;
import tbdp.tas.hbase.HBaseConnection;
import tbdp.tas.hbase.HBaseConnectionFactory;
import tbdp.tas.hbase.HbaseConnector;

import java.io.File;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by xbkaishui on 16/10/23.
 */
public class Utils {

    public static Connection getConnection() {
        try {
            //"jdbc:sqlserver://192.168.12.4;databaseName=MintMapping", "test1",
            //            "Test@9012"
            String db_connect_string = "jdbc:sqlserver://192.168.12.4;databaseName=MintMapping";
            String db_userid = "test1";
            String db_password = "Test@9012";
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            return DriverManager.getConnection(db_connect_string, db_userid, db_password);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }


    public static HTableInterface getHbaseTable(String table) {
        String zkNode = System.getProperty("hbase_znode", "/hbase-unsecure");
        String zkAddr = System.getProperty("hbase_zk",
                "zj-hz-bn01-hadoop-02,zj-hz-bn01-spark-03,zj-hz-bn01-spark-02,zj-hz-bn01-spark-01,zj-hz-bn01-hadoop-01");
        HBaseConnection conn = new HBaseConnection(zkAddr, zkNode);
        HbaseConnector hbaseConnector = HBaseConnectionFactory.instance().getHbaseConnector(conn);
        return hbaseConnector.getTable(table);
    }

    public static void main(String[] args) {
        List<String> data = new ArrayList<>();
        data.add("xxx");
        data.add("xxssss");
        data.add("haha");
        data.add("dddd");

        List<String> sub = data.subList(data.size() - 3, data.size());
        System.out.println(sub);
    }


    public static String readFile(String file) {
        try {
            return IOUtils.toString(new File(file).toURI().toURL());
        } catch (Exception e) {
            throw new RuntimeException("not found " + new File(file).getAbsolutePath());
        }
    }

}
